<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible.sync="showDialog" @close="btnCancel()">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      v-if="showDialog"
      ref="depFrom"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 90%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input
          v-model="formData.description"
          style="width: 90%"
          placeholder="1-50个字符"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="adddep">确定</el-button>
        <el-button size="small" @click="showDialog = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addRoleAPI, getRoleListAPI, updateRoleAPI } from '@/api'
export default {
  name: 'Addren',
  props: {},
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      let isRepeat = false
      if (!this.formData.id) {
        const { rows } = await getRoleListAPI({
          page: 1,
          pagesize: 1000000
        })
        isRepeat = rows.some((v) => v.name === value)
      }

      isRepeat ? callback(new Error(`角色里已经有${value}了`)) : callback()
    }
    return {
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,

            message: '角色名称要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: checkNameRepeat,
            trigger: 'blur'
          }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '角色描述要求1-50个字符',
            trigger: 'blur'
          }
        ]
      },
      formData: {
        name: '', // 部门名称
        description: '' // 部门编码
      },
      showDialog: false
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑角色' : '新增角色'
    }
  },

  methods: {
    adddep() {
      this.$refs.depFrom.validate(async(isOK) => {
        if (isOK) {
          if (this.formData.id) {
            await updateRoleAPI(this.formData)
          } else {
            await addRoleAPI(this.formData)
          }
          this.showDialog = false
          this.$message.success(`${this.showTitle}成功`)

          this.$emit('addRole')
        } else {
          this.$message.error('表单信息有误')
        }
      })
    },

    btnCancel() {
      // 重置数据  因为resetFields 只能重置 表单上的数据 非表单上的 比如 编辑中id 不能重置
      this.formData = {
        name: '',
        description: ''
      }

      // this.$refs.depFrom.resetFields()
    }
  }
}
</script>

<style scoped>
::v-deep .el-dialog__header {
  background-color: rgb(134, 33, 33);
}
</style>
>
